package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mapper.DeptMapper;
import com.itheima.mapper.EmpExprMapper;
import com.itheima.mapper.EmpMapper;
import com.itheima.pojo.*;
import com.itheima.service.EmpExprService;
import com.itheima.service.EmpService;
import lombok.RequiredArgsConstructor;

import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class EmpServiceImpl extends ServiceImpl<EmpMapper, Emp> implements EmpService {
    private final EmpExprService empExprService;
    private final DeptMapper deptMapper;
    private final EmpMapper empMapper;
    private final EmpExprMapper empExprMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveEmpAndExprList(Emp emp) {
        this.save(emp);
        List<EmpExpr> exprList = emp.getExprList();
        exprList.forEach(o -> o.setEmpId(emp.getId()));
        empExprService.saveBatch(exprList);
        return true;
    }

    @Override
    public Result getPageResult(EmpQueryParam param) {
        // 业务操作
        Page<Emp> page = Page.of(param.getPage(), param.getPageSize());
        String name = param.getName();
        Integer gender = param.getGender();
        LocalDate begin = param.getBegin();
        LocalDate end = param.getEnd();

        LambdaQueryWrapper<Emp> lqw = Wrappers.<Emp>lambdaQuery()
                .like(StringUtils.isNotBlank(name), Emp::getName, name)
                .eq(ObjectUtils.isNotEmpty(gender), Emp::getGender, gender)
                .between(ObjectUtils.isNotEmpty(begin) && ObjectUtils.isNotEmpty(end), Emp::getEntryDate, begin, end);
        this.page(page, lqw);

        List<Emp> records = page.getRecords();

        // List<Emp> list = records.stream().map(emp -> emp.setDeptName(
        //       deptMapper.selectList(Wrappers.<Dept>lambdaQuery().ge(ObjectUtils.isNull(Dept::getId)), emp.getDeptId()).getName)).toList();
        records.forEach(emp -> {
            Dept dept = deptMapper.selectOne(Wrappers.<Dept>lambdaQuery().eq(Dept::getId, emp.getDeptId()));
            if (dept != null) {
                emp.setDeptName(dept.getName());
            }
        });

        // 封装结果
        PageResult<Emp> pageResult = new PageResult<>(page.getTotal(), records);
        return Result.success(pageResult);
    }

    @Override
    public Result getEmpById(Integer id) {
        //LambdaQueryWrapper<Emp> wrapper = Wrappers.<Emp>lambdaQuery().eq(ObjectUtils.isNotEmpty(id), Emp::getId, id);
        Emp emp1 = this.getById(id);
        List<EmpExpr> list = empExprService.list(Wrappers.<EmpExpr>lambdaQuery().eq(EmpExpr::getEmpId, id));

        //Emp emp = empMapper.selectOne(wrapper);
        //List<EmpExpr> empExprs = empExprMapper.selectList(eq);
        emp1.setExprList(list);

        return Result.success(emp1);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result updateEmp(Emp emp) {
        emp.setUpdateTime(LocalDateTime.now());
        this.updateById(emp);

        empExprService.remove(Wrappers.<EmpExpr>lambdaQuery().eq(EmpExpr::getEmpId, emp.getId()));
        List<EmpExpr> exprList = emp.getExprList();
        if (exprList != null && !exprList.isEmpty()) {
            exprList.forEach(o -> o.setEmpId(emp.getId()));
        }
        empExprService.saveBatch(exprList);
        return Result.success();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteByIds(List<Integer> ids) {
        empMapper.deleteByIds(ids);
        // ids.forEach(id->empExprService.remove(Wrappers.<EmpExpr>lambdaQuery().eq( EmpExpr::getEmpId, id)));
        empExprService.remove(Wrappers.<EmpExpr>lambdaQuery().in(ids != null && !ids.isEmpty(), EmpExpr::getEmpId, ids));
    }
}
